package com.nidu.demo.config.executor.query;

import com.alibaba.cola.dto.MultiResponse;
import com.nidu.demo.config.dto.ConfigListQry;
import com.nidu.demo.config.convertor.ConfigConvertor;
import com.nidu.demo.config.dto.ConfigCO;
import com.nidu.demo.config.gateway.ConfigGateway;
import com.nidu.demo.config.model.Config;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * 配置列表查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class ConfigListQryExe {

    private final ConfigGateway gateway;
    private final ConfigConvertor convertor;

    public MultiResponse<ConfigCO> execute(ConfigListQry qry) {
        List<Config> configs = Collections.emptyList();
        
        // 根据查询条件获取配置列表
        if (qry.getConfigName() != null && !qry.getConfigName().trim().isEmpty()) {
            configs = gateway.getByNameLike(qry.getConfigName());
        } else if (qry.getStatus() != null) {
            configs = gateway.getByStatus(qry.getStatus());
        } else {
            // 分页查询
            configs = gateway.getPage(qry.getPageIndex(), qry.getPageSize());
        }

        // 转换为DTO返回
        List<ConfigCO> configCOs = convertor.toClientObjects(configs);

        return MultiResponse.of(configCOs);
    }
} 